package cn.jdy.plugins.admin;

import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.BrandDiscuss;
import cn.jdy.plugins.service.BrandDiscussService;
import cn.jdy.plugins.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 品牌评论
 *
 * @author wuzhiliang
 */
@Controller
@RequestMapping("/brandDiscuss")
public class BrandDiscussCtrl {

	@Autowired
	private BrandDiscussService brandDiscussService;
	@Autowired
	private CompanyService companyService;

	/**
	 * 品牌评论初始化
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView mv = new ModelAndView("/company/discussList.jsp");
		mv.addObject("companys", companyService.findCompanyList());
		return mv;
	}

	/**
	 * 查询品牌评论列表
	 *
	 * @param bo
	 */
	@RequestMapping("/query")
	@ResponseBody
	public Map<String, Object> query(BrandDiscuss bo) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", brandDiscussService.query(bo));
			int count = brandDiscussService.getCount(bo);
			jsonMap.put("iTotalRecords", count);
			jsonMap.put("iTotalDisplayRecords", count);
		} catch(Exception e) {
			jsonMap.put("status", 500);
			BaseLog.e(this.getClass(), "查询品牌评论列表出错", e);
		}
		return jsonMap;
	}

	/**
	 * 审核品牌评论信息
	 *
	 * @param id
	 * @param status
	 * @param advice
	 */
	@RequestMapping("/examine")
	@ResponseBody
	public Map<String, Object> examine(String id, String status, String advice) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			BrandDiscuss bo = brandDiscussService.find(id);
			//			bo.setStatus(status);
			//			bo.setAdvice(advice);
			brandDiscussService.update(bo);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "审核品牌评论信息出错", e);
		}

		return jsonMap;
	}


	/**
	 * 删除品牌评论
	 *
	 * @param id
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Map<String, Object> del(String id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			brandDiscussService.delete(id);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "删除品牌评论异常", e);
		}
		return jsonMap;
	}
	/**
	 * 删除品牌评论
	 *
	 * @param id
	 */
	@RequestMapping("/oper/{type}")
	@ResponseBody
	public Map<String, Object> oper(@PathVariable("type")char type, String id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			switch(type) {
				case 'P':
					brandDiscussService.updateSingleAttr(id, "status", "1");
					break;
				case 'R':
					brandDiscussService.updateSingleAttr(id, "status", "2");
					break;
				default: break;
			}
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "操作品牌评论异常", e);
		}
		return jsonMap;
	}
}
